package com.web.cms.pan.dao;

import com.blue.jdbc.BaseDao;
import com.web.cms.pan.model.ShareLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 共享日志数据库访问
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年04月09日
 */
@Repository("panShareLogDao")
public class ShareLogDao extends BaseDao<ShareLog>
{
	public ShareLogDao()
	{
	}
	
	@Override
	protected void query(StringBuilder sql, ShareLog param)
	{
		if (param != null)
		{
			if (param.getShareId() != null && param.getShareId() > 0)
				sql.append(" and a.share_id=:shareId");
				
			if (param.getUserId() != null && param.getUserId() > 0)
				sql.append(" and a.user_id=:userId");
				
		}
		sql.append(" order by a.id desc");
	}

	public void deleteByShare(List<Integer> shareIdList)
	{
		if (shareIdList == null || shareIdList.isEmpty())
			return;

		String param = StringUtils.join(shareIdList, ",");
		String sql = "delete from pan_share_log where share_id in (%s)";
		jdbcTemplate.update(String.format(sql, param));
	}

	public ShareLog create(Integer userId, Integer shareId, Date createTime)
	{
		ShareLog log = new ShareLog();
		log.setShareId(shareId);
		log.setUserId(userId);
		log.setCreateTime(createTime);
		return log;
	}

	public void add(Integer userId, Integer shareId)
	{
		ShareLog log = this.create(userId, shareId, new Date());
		this.save(log);
	}
	
}
